import os
from PIL import Image, ImageTk

class Book:
    """
    title = string
    path = path
    added_date = date
    read_date = date
    image = image data
    size = file size
    """
    
    def __init__(self, path, stat):
        self.title = ''
        self.path = path
        self.added_date = ''
        self.read_date = ''
        self.image = self._set_image(path) #setting self.image
        self.size = 0
        self.title = os.path.basename(path)
        pass
    
    def _set_image(self, path):
        print('running _set_image')
        
        if os.path.isdir(path):
            plist = sorted(os.listdir(path))
            image_path = ''
            for item in plist:
                ext = os.path.splitext(item)[-1]
                if ext == '.jpg' or ext == '.jpeg' or ext == '.gif' or ext == '.png' or ext == '.bmp':
                    image_path = os.path.join(path,item)
                    break
        elif os.path.isfile(path):
            return 'HIHIHI'
            pass
        
        (temp, photo) = self.getImageInfo(image_path)
        return photo
        
    
    def GetInfoByDic(self):
        return {'Title': self.title, 'Path': self.path, 'Added date': self.added_date, 'Read date': self.read_date, 'Image': self.image, 'Size': self.size, 'self': self}
    
    def getImageInfo(self, path, size = (200,200)):
        try:
            image_info = Image.open(path)
            image_info.thumbnail(size, Image.ANTIALIAS)
            photo = ImageTk.PhotoImage(image_info)
            return (image_info, photo)
        except FileNotFoundError:
            print("Can't find image")
            return